/*
 * (C)opyright 2010-2011, pengliu.2002@gmail.com
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 */

#ifndef _TJPEG2000PLUGIN_H_
#define _TJPEG2000PLUGIN_H_

#include "pluginbase.h"
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <gdk/gdkrgb.h>

class TJpeg2000Plugin : public nsPluginInstanceBase
{
public:
  TJpeg2000Plugin(NPP instance);
  ~TJpeg2000Plugin();

  NPBool init(NPWindow* window);
  void shut();
  NPBool isInitialized();

  NPError SetWindow(NPWindow *window);
  NPError NewStream(NPMIMEType type, NPStream *stream,
                    NPBool seekable, uint16_t *stype);
  NPError DestroyStream(NPStream *stream, NPError reason);
  int32_t WriteReady(NPStream *stream);
  int32_t Write(NPStream *stream, int32_t offset,
                int32_t len, void *buffer);
  uint16_t HandleEvent(void *event);
  NPError GetValue(NPPVariable variable, void *value);

private:
  static void OpenJpegErrorCallback(const char *msg, void *client_data);
  static void OpenJpegWarningCallback(const char *msg, void *client_data);
  static void OpenJpegInfoCallback(const char *msg, void *client_data);

  void DrawWindow(GdkDrawable *gdkWindow);
  NPBool Jpeg2000Decode();

private:
  NPWindow Window;
  unsigned char *Jpeg2000Buffer;
  int Jpeg2000BufferSize;
  int Jpeg2000DataLen;
  unsigned char *RawBuffer;

  enum {
    JPEG2000_BUFFER_SIZE_INIT = 512 * 1024,
    JPEG2000_BUFFER_SIZE_MAX = 16 * JPEG2000_BUFFER_SIZE_INIT,
    CHUNK_SIZE = 8 * 1024,
  };
};

#endif // _JPEG2000PLUGIN_H_
